//
//  AVGVisitView.swift
//  33-
//
//  Created by mac on 16/7/19.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit
    //定义代理协议
protocol   VisitorViewDelegate:NSObjectProtocol
   {
   //定义代理方发
    // 注册点击了
    func registerClick()
    
    // 登陆点击了
    func enterClick()


   }
    //访客视图
class AVGVisitView: UIView {
    // 3.定义代理属性
    // id <VisitorViewDelegate>
    weak var delegate: VisitorViewDelegate?
    
    // 一个view从storyboard或xib, 系统认为即要能用代码也要能从storyboard或xib
    required  init?(coder aDecoder: NSCoder)
    {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 实现init(frame)
    // 子类实现了构造函数,就不会继承父类的构造函数,系统会提示加上 init?(coder aDecoder: NSCoder) 构造函数
    override init(frame: CGRect)
    {
        super.init(frame: frame)
        backgroundColor = UIColor(white: 237/255.0, alpha: 1)
        self.setupUI()
    }
    //公开的方法
    func   setupvisitor(imagename:String ,message:String )
    {
    //隐藏房子
    houseview.hidden = true
    //将遮盖移动到view的后面
    self.sendSubviewToBack(coverview )
    //设置图片
    iconview.image = UIImage(named: imagename)
    //设置文字
    messageLabel.text = message
    

    }
    //转轮开始旋转
    func   startAnimation()
    {
    //使用核心动画
    let mian = CABasicAnimation(keyPath: "transform.rotation")
    //设置参数
    mian.toValue = 2 * M_PI
    mian.duration = 20
    mian.repeatCount = MAXFLOAT
    // 当切换到其他控制器系统会将核心动画结束,一结束默认就会将核心动画移除
    mian.removedOnCompletion = false
    // 将核心动画添加到图层上面
    iconview.layer.addAnimation(mian, forKey: nil )
    }
    //设置ui界面
    private   func setupUI()
    {
    
    //添加子控件
    self.addSubview(iconview)
    self.addSubview(coverview)
    self.addSubview(houseview)
    self.addSubview(messageLabel)
    self.addSubview(regietbutten)
    self.addSubview(loginbutten)
    
    //设置约束
    //使用纯代码进行约束，关闭其
    iconview.translatesAutoresizingMaskIntoConstraints = false
    coverview.translatesAutoresizingMaskIntoConstraints = false
    houseview.translatesAutoresizingMaskIntoConstraints = false
    messageLabel.translatesAutoresizingMaskIntoConstraints = false
    regietbutten.translatesAutoresizingMaskIntoConstraints = false
    loginbutten.translatesAutoresizingMaskIntoConstraints = false
        
    
    /*
        参数:
        1.item: 要约束的view
        2.attribute: 要约束对象的那个属性
        3.relatedBy: 参照关系
        4.toItem: 要参照的view
        5.attribute: 要参照view的那个属性
    */
    //iconview
    self.addConstraint(NSLayoutConstraint(item: iconview, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
    self.addConstraint(NSLayoutConstraint(item: iconview, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -40))
    // 遮盖
    self.addConstraint(NSLayoutConstraint(item: coverview, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconview, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
    self.addConstraint(NSLayoutConstraint(item: coverview, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconview, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))

    //房子
    self.addConstraint(NSLayoutConstraint(item: houseview, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconview, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
    self.addConstraint(NSLayoutConstraint(item: houseview, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconview, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
    //信息的label
    self.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
    self.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconview, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
    //label的宽度
    self.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 260))
    //注册按钮
    self.addConstraint(NSLayoutConstraint(item: regietbutten, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
    self.addConstraint(NSLayoutConstraint(item: regietbutten, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
    self.addConstraint(NSLayoutConstraint(item: regietbutten, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
    self.addConstraint(NSLayoutConstraint(item: regietbutten, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
    //登录按钮
    self.addConstraint(NSLayoutConstraint(item: loginbutten, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
    self.addConstraint(NSLayoutConstraint(item: loginbutten, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
    self.addConstraint(NSLayoutConstraint(item: loginbutten, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
    self.addConstraint(NSLayoutConstraint(item: loginbutten, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))

    }
    
    //懒加载
    //轮番器
    private  lazy var iconview:UIImageView = UIImageView(image: UIImage(named:"visitordiscover_feed_image_smallicon"))
    
    //增加遮盖
    private  lazy  var  coverview:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    //小房子
    private  lazy   var houseview:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    //消息label
    private  lazy   var  messageLabel:UILabel =
    {
    let  label = UILabel(frame: CGRectZero)
    label.textAlignment = NSTextAlignment.Left
    label.font =  UIFont.systemFontOfSize(20)
    label.textColor = UIColor.darkGrayColor()
    label.numberOfLines = 0
    label.text = "钓鱼钓是中国的，南海是中国的"
    //让控制自己去调整大小
    label.sizeToFit()
    
    return  label
    
    }()
    
    //注册按钮
    private lazy  var  regietbutten:UIButton =
    {
    let regietbutten = UIButton(type: UIButtonType.Custom)
    regietbutten.setTitle("注册", forState: UIControlState.Normal)
    regietbutten.setTitleColor(UIColor.redColor(), forState: UIControlState.Normal)
    regietbutten.titleLabel?.font = UIFont.systemFontOfSize(12)
    regietbutten.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
    //添加点击事件
    regietbutten.addTarget(self, action: "registerButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
    return  regietbutten
    
    }()
    
    //登录按钮
    private  lazy   var  loginbutten:UIButton =
    {
    let  loginbutten = UIButton(type: UIButtonType.Custom)
    loginbutten.setTitle("登录", forState: UIControlState.Normal)
    loginbutten.setTitleColor(UIColor.redColor(), forState: UIControlState.Normal)
    loginbutten.titleLabel?.font = UIFont.systemFontOfSize(12)
    loginbutten.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
    //添加点击事件
    loginbutten.addTarget(self , action: "loginButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
    return  loginbutten
    
    }()
    //点击事件
    @objc  private  func  registerButtonClick()
    {
    
        // 4.调用代理的方法
        delegate?.registerClick()
    }
    //登录按钮的点击事件
    @objc  private   func  loginButtonClick()
    {
        // 4.调用代理的方法
        delegate?.enterClick()

    
    
    }
}

 